package com.vipbbo.security.springboot.dao;

import com.vipbbo.security.springboot.model.PermissionDto;
import com.vipbbo.security.springboot.model.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bbo(zbw)
 * Description
 * @Date 2021/4/26
 */

@Repository
public class UserDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    // 根据账号查询用户信息
    public UserDto getUserByUsername(String username){

        // 连接数据库查询用户
        String sql = "select id,username,password,fullname from t_user where username = ?";
        List<UserDto> list = jdbcTemplate.query(sql, new Object[]{username},new
                BeanPropertyRowMapper<>(UserDto.class));
        if (list == null && list.size() <= 0){
            return null;
        }
        return list.get(0);
    }

    // 根据用户id查询用户的权限
    public List<String> findPermissionsByUserId(String userId){
        String sql = "select * from t_permission where id in(\n" +
                "\tselect permission_id from t_role_permission where role_id in(\n" +
                "\n" +
                "\tselect role_id from t_user_role where user_id =? \n" +
                ")\n" +
                ")";
        List<PermissionDto> list = jdbcTemplate.query(sql, new Object[]{userId},
                new BeanPropertyRowMapper<>(PermissionDto.class));
        List<String> permissions = new ArrayList<>();
        list.forEach(c ->permissions.add(c.getCode()));

        return permissions;
    }
}
